package com.ptadmin.report.mws;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amazonaws.mws.MarketplaceWebServiceException;
import com.amazonaws.mws.model.GetReportRequest;
import com.amazonaws.mws.model.GetReportResponse;
import com.amazonaws.mws.model.ResponseMetadata;

public class GetReportWrapper extends ReportWrapperBase {

	private static final Logger logger = LoggerFactory.getLogger(ReportWrapperBase.class);
	
	private GetReportRequest request;
	private GetReportResponse response;
	
	public void buildRequest(String merchantId, String mwsAuthToken, String reportId, String saveFilePath) {
		this.request = new GetReportRequest()
				.withMerchant(merchantId)
				.withMWSAuthToken(mwsAuthToken)
				.withReportId(reportId);

		OutputStream report;
		try {
			logger.info(String.format("reportId: %s would be saved as file %s...", reportId, saveFilePath));
			report = new FileOutputStream(saveFilePath);
			request.setReportOutputStream(report);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public void invokeRequest() {
		try {
			this.response = this.service.getReport(request);
		} catch (MarketplaceWebServiceException ex) {
			System.out.println("Caught Exception: " + ex.getMessage());
			System.out.println("Response Status Code: " + ex.getStatusCode());
			System.out.println("Error Code: " + ex.getErrorCode());
			System.out.println("Error Type: " + ex.getErrorType());
			System.out.println("Request ID: " + ex.getRequestId());
			System.out.print("XML: " + ex.getXML());
			System.out.println("ResponseHeaderMetadata: " + ex.getResponseHeaderMetadata());
		} catch (Exception ex) {
			System.out.print(ex.toString());
		}
	}

	public void printResponse() {
		System.out.println("GetReport Action Response");
		System.out.println("=============================================================================");
		System.out.println();

		System.out.print("    GetReportResponse");
		System.out.println();
		System.out.print("    GetReportResult");
		System.out.println();
		System.out.print("            MD5Checksum");
		System.out.println();
		System.out.print("                " + response.getGetReportResult().getMD5Checksum());
		System.out.println();
		if (response.isSetResponseMetadata()) {
			System.out.print("        ResponseMetadata");
			System.out.println();
			ResponseMetadata responseMetadata = response.getResponseMetadata();
			if (responseMetadata.isSetRequestId()) {
				System.out.print("            RequestId");
				System.out.println();
				System.out.print("                " + responseMetadata.getRequestId());
				System.out.println();
			}
		}
		System.out.println();

		System.out.println("Report");
		System.out.println("=============================================================================");
		System.out.println();
		// System.out.println(request.getReportOutputStream().toString());
		System.out.println();

		System.out.println(response.getResponseHeaderMetadata());
		System.out.println();
	}
}
